//
//  LBWStatusOriginalView.swift
//  weibo
//
//  Created by Limonwei on 15/12/9.
//  Copyright © 2015年 heima. All rights reserved.
//

//原创微博视图部分

import UIKit
import SnapKit

class LBWStatusOriginalView: UIView {
    
    //当前视图的底部约束
    var bottomConstraint: Constraint?
    
    var statusViewModel: LBWStatusViewModel? {
        
        didSet{
            //设置头像
            iconView.sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
            
            //设置昵称
            nameLabel.text = statusViewModel?.status?.user?.name

            //nameLabel.text = "我叫xxxx"
            
            //设置时间
            timeLabel.text = statusViewModel?.creatAtText
            
            //设置来源
            sourceLabel.text = statusViewModel?.sourceText
            
            //设置会员图标
            vipImageView.image = statusViewModel?.vipImage
            
            //设置认证
            verifiedImageView.image = statusViewModel?.verifiedImage
            
            //设置微博内容
            //contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalStatusAttr
            contentLabel.linkResults = statusViewModel?.originalLinkResults
            
            //设置原创微博的配图 先去除旧的约束
            bottomConstraint?.uninstall()
            
            //如果有配图
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0 {
                
                //设置数据
                pictureView.pic_urls = pic_urls
                //显示控件
                pictureView.hidden = false
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView).offset(LBWStatusCellMargin).constraint
                })
            }else{
                
                pictureView.hidden = true
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(LBWStatusCellMargin).constraint
                })
            }
            
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //懒加载控件
    //头像控件
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.redColor(), fontSize: LBWStatusNameFont)
    
    //时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: LBWStatusTimeFont)
    
    //来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: LBWStatusSourceFont)
    
    //会员图标
    private lazy var vipImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //认证图标
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //微博内容
    private lazy var contentLabel: LBWStatusLabel = LBWStatusLabel(textColor: UIColor.darkGrayColor(), fontSize: 15, maxWidth: SCREENW - 2*LBWStatusCellMargin)
    
    //原创微博的配图控件
    private lazy var pictureView: LBWStatusPictureView = {
       
        let view = LBWStatusPictureView()
        
        view.backgroundColor = self.backgroundColor
        
        return view
        
    }()
    
    
    //设置UI
    private func setupUI(){
        
        //添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束 约束一定要设置正确 不然不恩能够显示出来
        //头像约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(LBWStatusCellMargin)
            make.leading.equalTo(self).offset(LBWStatusCellMargin)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        
        //昵称约束
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(LBWStatusCellMargin)
        }
        
        
        //会员约束
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(LBWStatusCellMargin)
        }
        
        //时间约束
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
        //来源约束
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(LBWStatusCellMargin)
        }
        
        //认证约束
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        //微博内容约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(LBWStatusCellMargin)
            make.trailing.equalTo(self).offset(-LBWStatusCellMargin)
        }
        
        //配图的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(LBWStatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        //关键点  当亲控件的高与内容的高度是一致的
        snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(LBWStatusCellMargin)
        }
        
    }
    
}



























